home *** CD-ROM | disk | FTP | other *** search
- <?xml version="1.0" encoding="UTF-8" standalone="no"?>
- <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
- <html xmlns="http://www.w3.org/1999/xhtml">
- <head>
- <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
- <title>10.6. Extending The Text Box Script</title>
- <link rel="stylesheet" href="gimp-help-plain.css" type="text/css" />
- <link rel="stylesheet" href="gimp-help-screen.css" type="text/css" />
- <meta name="generator" content="DocBook XSL Stylesheets V1.66.1" />
- <link rel="start" href="index.html" title="GIMP User Manual" />
- <link rel="up" href="ch02s10.html" title="10. A Script-Fu Tutorial" />
- <link rel="prev" href="ch02s10s05.html" title="10.5. Giving Our Script Some Guts" />
- <link rel="next" href="ch02s11.html" title="11. Getting Unstuck" />
- </head>
- <body>
- <div xmlns="" class="navheader">
- <table width="100%" summary="Navigation header">
- <tr>
- <th colspan="3" align="center" id="chaptername">10. A Script-Fu Tutorial</th>
- </tr>
- <tr>
- <td width="20%" align="left"><a accesskey="p" href="ch02s10s05.html">Prev</a> </td>
- <th width="60%" align="center" id="sectionname">10.6. Extending The Text Box Script</th>
- <td width="20%" align="right"> <a accesskey="n" href="ch02s11.html">Next</a></td>
- </tr>
- </table>
- <hr />
- </div>
- <div class="sect2" lang="en" xml:lang="en">
- <div class="titlepage">
- <div>
- <div>
- <h3 class="title"><a id="id3431098"></a>10.6. Extending The Text Box Script</h3>
- </div>
- </div>
- </div>
- <div class="simplesect" lang="en" xml:lang="en">
- <div class="titlepage">
- <div>
- <div>
- <h4 class="title"><a id="id3431105"></a>Handling Undo Correctly</h4>
- </div>
- </div>
- </div>
- <p>
- When creating a script, you want to give your users the
- ability to undo their actions, should they make a
- mistake. This is easily accomplished by calling the functions
- <tt class="code">gimp-undo-push-group-start</tt> and
- <tt class="code">gimp-undo-push-group-end</tt> around
- the code that manipulates the image. You can think of them as
- matched statements that let Gimp know when to start and stop
- recording manipulations on the image, so that those
- manipulations can later be undone.
- </p>
- <p>
- If you are creating a new image entirely, it doesn't make
- sense to use these functions because you're not changing an
- existing image. However, when you are changing an existing
- image, you most surely want to use these functions.
- </p>
- <p>
- Undoing a script works nearly flawlessly when using these
- functions.
- </p>
- </div>
- <div class="simplesect" lang="en" xml:lang="en">
- <div class="titlepage">
- <div>
- <div>
- <h4 class="title"><a id="id3431142"></a>Extending The Script A Little More</h4>
- </div>
- </div>
- </div>
- <p>
- Now that we have a very handy-dandy script to create text
- boxes, let's add two features to it:
- </p>
- <div class="itemizedlist">
- <ul type="disc">
- <li>
- <p>
- Currently, the image is resized to fit exactly around the
- text -- there's no room for anything, like drop shadows or
- special effects (even though many scripts will automatically
- resize the image as necessary). Let's add a buffer around
- the text, and even let the user specify how much buffer to
- add as a percentage of the size of the resultant text.
- </p>
- </li>
- <li>
- <p>
- This script could easily be used in other scripts that work
- with text. Let's extend it so that it returns the image and
- the layers, so other scripts can call this script and use
- the image and layers we create.
- </p>
- </li>
- </ul>
- </div>
- </div>
- <div class="simplesect" lang="en" xml:lang="en">
- <div class="titlepage">
- <div>
- <div>
- <h4 class="title"><a id="id3431179"></a>Modifying The Parameters And The Registration Function</h4>
- </div>
- </div>
- </div>
- <p>
- To let the user specify the amount of buffer, we'll add a
- parameter to our function and the registration function:
- </p>
- <pre class="programlisting">
- (define (script-fu-text-box inTest inFont inFontSize inTextColor)
- (let*
- (
- ; define our local variables
- ; create a new image:
- (theImageWidth 10)
- (theImageHeight 10)
- (theImage (car
- (gimp-image-new
- theImageWidth
- theImageHeight
- RGB
- )
- )
- )
- (theText) ;a declaration for the text
- ;we create later
-
- (theBuffer) ;<span class="bold"><b>added</b></span>
- (theLayer
- (car
- (gimp-layer-new
- theImage
- theImageWidth
- theImageHeight
- RGB_IMAGE
- "layer 1"
- 100
- NORMAL
- )
- )
- )
- ) ;end of our local variables
-
- <i class="replaceable"><tt>[Code here]</tt></i>
- )
- </pre>
- <pre class="programlisting">
-
- (script-fu-register
- "script-fu-text-box" ;func name
- "<Toolbox>/Xtns/Script-Fu/Text/Text Box" ;menu pos
- "Creates a simple text box, sized to fit\
- around the user's choice of text,\
- font, font size, and color." ;description
- "Michael Terry" ;author
- "copyright 1997, Michael Terry" ;copyright notice
- "October 27, 1997" ;date created
- "" ;image type that the script works on
- SF-VALUE "Text:" "\"Text Box\"" ;a text variable
- SF-VALUE "Font:" "\"Charter\"" ;a text variable
- SF-VALUE "Font size:" "45" ;a text variable
- SF-COLOR "Color:" '(0 0 0) ;color variable
- SF-VALUE "Buffer amount" "35" ; 0-100 % of text
- )
-
- </pre>
- </div>
- <div class="simplesect" lang="en" xml:lang="en">
- <div class="titlepage">
- <div>
- <div>
- <h4 class="title"><a id="id3431252"></a>Adding The New Code</h4>
- </div>
- </div>
- </div>
- <p>
- We're going to add code in two places: right before we resize
- the image, and at the end of the script (to return the new
- image, the layer and the text).
- </p>
- <p>
- After we get the text's height and width, we need to resize
- these values based on the buffer amount specified by the
- user. We won't do any error checking to make sure it's in the
- range of 0-100% because it's not life-threatening, and because
- there's no reason why the user can't enter a value like "200"
- as the percent of buffer to add.
- </p>
- <pre class="programlisting">
- (set! theBuffer (* theImageHeight (inBufferAmount 100) ) )
-
- (set! theImageHeight (+ theImageHeight theBuffer theBuffer) )
- (set! theImageWidth (+ theImageWidth theBuffer theBuffer) )
- </pre>
- <p>
- All we're doing here is setting the buffer based on the height
- of the text, and adding it twice to both the height and width
- of our new image. (We add it twice to both dimensions because
- the buffer needs to be added to both sides of the text.)
- </p>
- <p>
- Now that we have resized the image to allow for a buffer, we
- need to center the text within the image. This is done by
- moving it to the (x, y) coordinates of (<tt class="varname">theBuffer</tt>,
- <tt class="varname">theBuffer</tt>). I added this line after
- resizing the layer and the image:
- </p>
- <pre class="programlisting">
- (gimp-layer-set-offsets theText theBuffer theBuffer)
- </pre>
- <p>
- Go ahead and save your script, and try it out after refreshing
- the database.
- </p>
- <p>
- All that is left to do is return our image, the layer, and the
- text layer. After displaying the image, we add this line:
- </p>
- <pre class="programlisting">
- (list theImage theLayer theText)
- </pre>
- <p>
- This is the last line of the function, making this list
- available to other scripts that want to use it.
- </p>
- <p>
- To use our new text box script in another script, we could
- write something like the following:
- </p>
- <pre class="programlisting">
- (set! theResult (script-fu-text-box
- "Some text"
- "Charter" "30"
- '(0 0 0)
- "35"
- )
- )
- (gimp-image-flatten (car theResult))
- </pre>
- <p>
- Congratulations, you are on your way to your Black Belt of Script-Fu!
- </p>
- </div>
- </div>
- <div class="navfooter">
- <hr />
- <table width="100%" summary="Navigation footer">
- <tr>
- <td width="40%" align="left"><a accesskey="p" href="ch02s10s05.html">Prev</a> </td>
- <td width="20%" align="center">
- <a accesskey="u" href="ch02s10.html">Up</a>
- </td>
- <td width="40%" align="right"> <a accesskey="n" href="ch02s11.html">Next</a></td>
- </tr>
- <tr>
- <td width="40%" align="left" valign="top">10.5. Giving Our Script Some Guts </td>
- <td width="20%" align="center">
- <a accesskey="h" href="index.html">Home</a>
- </td>
- <td width="40%" align="right" valign="top"> 11. Getting Unstuck</td>
- </tr>
- </table>
- </div>
- </body>
- </html>
-